﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Geometry
{
    /// <summary>
    /// This class represents a polygon in a three dimensional space.
    /// </summary>
    [Serializable]
    public class Polygon3D : GeometricObject, IPolygon3D
    {
        /// <summary>
        /// Saves the data for the polygon.
        /// </summary>
        private List<Point3D> polygonData;

        /// <summary>
        /// Initializes a new instance of the <see cref="Polygon3D"/> class.
        /// </summary>
        public Polygon3D()
        {
            this.polygonData = new List<Point3D>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Polygon3D"/> class.
        /// </summary>
        /// <param name="polygon">The polygon to clone.</param>
        public Polygon3D(Polygon3D polygon)
        {
            if (polygon == (Polygon3D) null)
            {
                throw new ArgumentNullException("polygon");
            }

            this.polygonData = polygon.PolygonData;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Polygon3D"/> class.
        /// </summary>
        /// <param name="data">The points of the polygon in form of an array.</param>
        public Polygon3D(Point3D[] data)
        {
            if (data == (Point3D[]) null)
            {
                throw new ArgumentNullException("data");
            }

            this.polygonData = new List<Point3D>();

            for (int i = 0; i < data.Length; i++)
            {
                this.polygonData.Add(data[i]);
            }
        }

        /// <summary>
        /// Gets or sets the polygon data in form of a list.
        /// </summary>
        /// <value>The polygon data.</value>
        public List<Point3D> PolygonData
        {
            get { return polygonData; }
            set { polygonData = value; }
        }

        /// <summary>
        /// Adds a point to the polygon.
        /// </summary>
        /// <param name="point">The point to add.</param>
        public void AddPoint(Point3D point)
        {
            this.polygonData.Add(point);
        }

        /// <summary>
        /// Gets the point at a specified position.
        /// </summary>
        /// <param name="index">The index of the point in the point list.</param>
        /// <returns>The point specified at the specified position.</returns>
        public Point3D GetPoint(int index)
        {
            return this.polygonData[index];
        }

        /// <summary>
        /// Gets the number of points of the current polygon.
        /// </summary>
        /// <returns>The number of points of the current polygon.</returns>
        public int GetNumberOfPoints()
        {
            return this.polygonData.Count;
        }

        /// <summary>
        /// Deletes the point of the polygon.
        /// </summary>
        /// <param name="index">The index of the point which should be deleted.</param>
        public void DeletePoint(int index)
        {
            this.polygonData.RemoveAt(index);
        }

        /// <summary>
        /// Scales the polygon with a specified factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor for x and y position.</param>
        public void ScalePolygon(double scaleFactor)
        {
            foreach (Point3D p in this.polygonData)
            {
                p.ScaleTransformation(scaleFactor);
            }
        }

        /// <summary>
        /// Scales the polygon with specified factors.
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x position.</param>
        /// <param name="scaleFactorY">The scale factor for the y position.</param>
        /// <param name="scaleFactorZ">The scale factor for the z position.</param>
        public void ScalePolygon(double scaleFactorX, double scaleFactorY, double scaleFactorZ)
        {
            foreach (Point3D p in this.polygonData)
            {
                p.ScaleTransformation(scaleFactorX, scaleFactorY, scaleFactorZ);
            }
        }

        /// <summary>
        /// Translates the polygon with the specified parameters.
        /// </summary>
        /// <param name="translateValue">The value with which the polygon has to be translated.</param>
        public void TranslatePolygon(double translateValue)
        {
            foreach (Point3D p in this.polygonData)
            {
                p.TranslateTransformation(translateValue);
            }
        }

        /// <summary>
        /// Translates the polygon in x and y direction.
        /// </summary>
        /// <param name="translateValueX">The translation value for x the axis.</param>
        /// <param name="translateValueY">The translation value for y the axis.</param>
        /// <param name="translateValueZ">The translation value for z the axis.</param>
        public void TranslatePolygon(double translateValueX, double translateValueY, double translateValueZ)
        {
            foreach (Point3D p in this.polygonData)
            {
                p.TranslateTransformation(translateValueX, translateValueY, translateValueZ);
            }
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first polygon.</param>
        /// <param name="b">The second polygon.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Polygon3D a, Polygon3D b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            if (a.GetNumberOfPoints() != b.GetNumberOfPoints())
            {
                return false;
            }

            for (int i = 0; i < a.GetNumberOfPoints(); i++)
            {
                if (a.GetPoint(i) != b.GetPoint(i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first polygon.</param>
        /// <param name="b">The second polygon.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Polygon3D a, Polygon3D b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Compares the current instance of a polygon with another polygon object. 
        /// </summary>
        /// <param name="compare">The line to compare.</param>
        /// <returns>True if the position of the polygons are even otherwise the method will 
        /// return false.</returns>
        public bool CompareTo(Polygon3D compare)
        {
            if (compare == (Polygon3D) null)
            {
                throw new ArgumentNullException("compare");
            }

            return this == compare;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = "";

            foreach (Point3D p in this.polygonData)
            {
                result += p.ToString() + "|";
            }

            return result;
        }
    }
}